package com.hs.controller;

import com.hs.entity.Ggao;
import com.hs.entity.Grade;
import com.hs.entity.Result;
import com.hs.service.GgaoService;
import com.hs.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ggao")
public class GgaoController {

    @Autowired
    //private GradeService gradeService;
    private GgaoService ggaoService;
    //跳转到发布公告列表
    @RequestMapping("/fggao")
    public String ggaoList(){
        return "teacher/fggao-list";
    }
    //获取公告列表信息
    @RequestMapping("/list/fggao")
    @ResponseBody
    public Map ggaoListJson(String searchKey){
        List<Ggao> list=ggaoService.findGgaoList("%"+searchKey+"%");
        Map<String,Object> map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }

    //跳转到编辑公告界面
    @RequestMapping("/editggao")
    public String editggao(Integer id, Model model ){
        model.addAttribute("id",id);
        Ggao ggao = ggaoService.findGgaoById(id);
        model.addAttribute("ggaoInfo",ggao);
        return "teacher/fggao-edit";
    }

    //保存公告编辑
    @RequestMapping("/saveeditggao")
    @ResponseBody
    public Result saveeditggao(Integer id, String title, String text){
        Integer row=ggaoService.saveeditggao(id,title,text);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("保存成功");
        }else {
            result.setCode(10);
            result.setMsg("保存失败");
        }

        return result;
    }

    //跳转到发布公告界面
    @RequestMapping("/addggao")
    public String addggao(){
        return "teacher/fggao-add";
    }
    //保存添加公告
    @RequestMapping("/saveggao")
    @ResponseBody
    public Result saveggao(String teacherid,String title, String text){
        Integer row=ggaoService.saveggao(teacherid,title,text);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("保存成功，发布待审核");
        }else {
            result.setCode(10);
            result.setMsg("发布失败");
        }

        return result;
    }

    //查看公告内容
    @RequestMapping("/seeggao")
    public String seeggao(Integer id, Model model ){
        model.addAttribute("id",id);
        Ggao ggao = ggaoService.findGgaoById(id);
        model.addAttribute("ggaoInfo",ggao);
        return "teacher/ggao-see";
    }

    //撤回公告
    @RequestMapping("/cancleggao")
    @ResponseBody
    public Result cancleggao(Integer id){
        Integer row=ggaoService.cancleggao(id);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("撤回成功");
        }else {
            result.setCode(10);
            result.setMsg("撤回失败");
        }
        return result;
    }


    //跳转到审核公告列表
    @RequestMapping("/sggao")
    public String sggaoList(){
        return "teacher/sggao-list";
    }
    //审核通过公告
    @RequestMapping("/passggao")
    @ResponseBody
    public Result passggao(Integer id){
        Integer row=ggaoService.passggao(id);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("通过成功");
        }else {
            result.setCode(10);
            result.setMsg("通过失败");
        }
        return result;
    }
    //审核通过公告
    @RequestMapping("/nopassggao")
    @ResponseBody
    public Result nopassggao(Integer id){
        Integer row=ggaoService.nopassggao(id);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("驳回成功");
        }else {
            result.setCode(10);
            result.setMsg("驳回失败");
        }
        return result;
    }
    //跳转到学生查看公告列表
    @RequestMapping("/stuggao")
    public String stuggao(){
        return "teacher/stuggao-list";
    }
    //学生获取公告列表信息
    @RequestMapping("/stuggaolist")
    @ResponseBody
    public Map stuggaolist(String searchKey){
        List<Ggao> list=ggaoService.findGgaoList2("%"+searchKey+"%");
        Map<String,Object> map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }
}
